#include <iostream>
#include "shared_ptr.h"
using namespace std;


struct Date{
	int _year;
	int _month;
	int _day;

	Date(int year = 1, int month = 1, int day = 1)
		: _year(year)
		, _month(month)
		, _day(day)
	{}

	~Date()
	{
		cout << "~Date()" << endl;
	}
};



//int main(){
//	qian::shared_ptr<Date> sp1(new Date);
//	sp1->_year++;
//
//	qian::shared_ptr<Date> sp2(sp1);
//	qian::shared_ptr<Date> sp3(new Date);
//
//	//sp3 = sp1; //用第一次实现的拷贝，会产生内存泄露，new了两次但是只析构了一次
//	//sp2 = sp3;
//
//	//自己给自己赋值，也存在一定的问题，因此需要对operator=()作出一定修改
//	sp3 = sp3;
//	sp1 = sp2;
//
//
//	return 0;
//}



// 循环引用
struct ListNode
{
	int _data;

	//std::shared_ptr<ListNode> _next;
	//std::shared_ptr<ListNode> _prev;
	//用weak_ptr就可以避免内存泄露
	std::weak_ptr<ListNode> _next;
	std::weak_ptr<ListNode> _prev;


	~ListNode()
	{
		cout << "~ListNode()" << endl;
	}
};



int main()
{
	// 下面这就是循环引用 --> 内存泄露
	std::shared_ptr<ListNode> n1(new ListNode);
	std::shared_ptr<ListNode> n2(new ListNode);

	//n1->_next = n2;
	//n2->_prev = n1; //互相指向，就会出现内存泄露

	cout << n1.use_count() << endl;
	cout << n2.use_count() << endl;

	n1->_next = n2;
	n2->_next = n1;

	cout << n1.use_count() << endl;
	cout << n2.use_count() << endl;



	return 0;
}